/**
 * 
 */
package org.stars.daostars.exceptions.translators;

import java.sql.SQLException;
import java.util.Hashtable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.stars.dao.exception.DaoException;
import org.stars.daostars.SQLExceptionTranslator;
import org.stars.daostars.exceptions.ChildRowFoundException;
import org.stars.daostars.exceptions.ColumnIsNullException;
import org.stars.daostars.exceptions.UniqueConstraintViolatedException;
import org.stars.database.DatabaseInformation;


/**
 * Gestore delle eccezioni per database oracle.
 * 
 * Errori supportati
 * 
 * <table border=1>
 * <tr>
 * 		<td>ORA-0001</td>
 * 		<td>Unique constraint violated</td>
 * 		<td>UniqueConstraintViolatedException</td>
 * </tr>
 * <tr>
 * 		<td>ORA-01400</td> 
 * 		<td>impossibile inserire NULL in (Utente.Tabella.Colonna)</td>
 * 		<td>ColumnIsNullException</td>
 * </tr>
 * <tr>
 * 		<td>ORA-02292</td> 
 * 		<td>integrity constraint constraint name violated - child record found</td>
 * 		<td>ColumnIsNullException</td>
 * </tr>
 * </table>
 * 
 * @author Francesco Benincasa (908099)
 * 
 */
public class OracleSQLExceptionTranslator implements SQLExceptionTranslator {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6803862097938540459L;
	
	protected Hashtable<Integer, Class<? extends DaoException>> codici;

	public OracleSQLExceptionTranslator() {
		codici = new Hashtable<Integer, Class<? extends DaoException>>();

		codici.put(1, UniqueConstraintViolatedException.class);
		codici.put(1400, ColumnIsNullException.class);
		codici.put(2292, ChildRowFoundException.class);		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * it.insiel.daostars.SQLExceptionTranslator#translate(java.lang.String,
	 * java.sql.SQLException, it.insiel.database.DatabaseInformation)
	 */
	@Override
	public DaoException translate(String sql, SQLException e, DatabaseInformation dbInfo) {

		try {
			if (codici.containsKey(e.getErrorCode())) {
				return DefaultSQLExceptionTranslatorImpl.createException(codici.get(e.getErrorCode()), e);
			}
		} catch (Exception e1) {
			Log log = LogFactory.getLog(OracleSQLExceptionTranslator.class);
			
			log.error("Unable to translate exception "+e1.getMessage());
		}
		return null;
	}

}
